package com.xbongbong.callback.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.callback.service.ConnectorConfigService;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.sys.domain.entity.ConnectorConfigEntity;
import com.xbongbong.sys.enums.DdModelEnum;
import com.xbongbong.sys.model.ConnectorConfigModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 连接器配置
 * 创建时间 2021/5/25 4:34 PM
 * 修改时间 2021/5/25 4:34 PM
 * @author chy
 */
@Service("connectorConfigService")
public class ConnectorConfigServiceImpl implements ConnectorConfigService {
    private static final Logger LOG = LoggerFactory.getLogger(ConnectorConfigServiceImpl.class);
    @Resource
    private ConnectorConfigModel connectorConfigModel;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    @Override
    public void save(JSONObject plainTextJson) throws XbbException {
        String corpid = plainTextJson.getString("AuthCorpId");
        String modelId = plainTextJson.getString("MainEventModelId");
        String subAuth = plainTextJson.getString("SubAuth");
        JSONObject targetApp = plainTextJson.getJSONObject("targetApp");
        String appId = targetApp.getString("appId");
        String appName = targetApp.getString("appName");
        String connectorId = targetApp.getString("connectorId");
        String actionId = targetApp.getString("actionId");
        String actionName = targetApp.getString("actionName");

        ConnectorConfigEntity connectorConfig = connectorConfigModel.getByModelId(corpid, modelId);

        if (Objects.isNull(connectorConfig)) {
            connectorConfig = new ConnectorConfigEntity();
        }
        connectorConfig.setCorpid(corpid);
        connectorConfig.setConnectorId(connectorId);
        connectorConfig.setModelId(modelId);
        connectorConfig.setSubAuth(subAuth);
        connectorConfig.setAppId(appId);
        connectorConfig.setAppName(appName);
        connectorConfig.setActionId(actionId);
        connectorConfig.setActionName(actionName);
        connectorConfigModel.save(connectorConfig);
    }

    @Override
    public boolean hasTrigger(String corpid, XbbRefTypeEnum xbbRefTypeEnum) {
        List<String> modelIdList = DdModelEnum.getModelId(xbbRefTypeEnum);
        List<String> dbModelIdList = getModelIdList(corpid);
        modelIdList.retainAll(dbModelIdList);
        return modelIdList.size() > 0;
    }

    @Override
    public boolean hasStandardTrigger(String corpid, XbbRefTypeEnum xbbRefTypeEnum) {
        String modelId = DdModelEnum.getStandardModelId(xbbRefTypeEnum);
        List<String> dbModelIds = getModelIdList(corpid);
        return dbModelIds.contains(modelId);
    }

    private List<String> getModelIdList(String corpid) {
        String triggerIdListStr = paasRedisHelper.getValue(RedisPrefixConstant.DD_MODEL, corpid);
        if (!StringUtil.isEmpty(triggerIdListStr)) {
            try {
                return JsonHelperUtil.parseArray(triggerIdListStr, String.class);
            } catch (Exception e) {
                return new ArrayList<>();
            }
        }
        List<String> modelIdList = new ArrayList<>();
        List<ConnectorConfigEntity> connectorConfigs = connectorConfigModel.getByCorpId(corpid);
        for (ConnectorConfigEntity connectorConfig : connectorConfigs) {
            String modelId = connectorConfig.getModelId();
            if (!StringUtil.isEmpty(modelId)) {
                modelIdList.add(modelId);
            }
        }
        paasRedisHelper.setValue(RedisPrefixConstant.DD_MODEL, corpid, modelIdList, RedisTimeConstant.TINY_DURATION);
        return modelIdList;
    }
}
